home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyc (Python 2.6)
-
- import warnings
- warnings.filterwarnings('ignore', 'apt API not stable yet', FutureWarning)
- import apt
- import apt_pkg
- import os.path as os
- import shutil
- import subprocess
- import thread
- import time
- import gettext
- import sys
- import string
- import tempfile
- import re
- import FontConfig
- from gettext import gettext as _
- from LocaleInfo import LocaleInfo
- from LangCache import *
-
- class LanguageSelectorBase(object):
- ''' base class for language-selector code '''
-
- def __init__(self, datadir = ''):
- self._datadir = datadir
- self._localeinfo = LocaleInfo('%s/data/languagelist' % self._datadir)
- self._cache = None
-
-
- def openCache(self, progress):
- self._cache = LanguageSelectorPkgCache(self._localeinfo, progress)
-
-
- def getMissingLangPacks(self):
- '''
- return a list of langauge packs that are not installed
- but should be installed
- '''
- missing = []
- for langInfo in self._cache.getLanguageInformation():
- trans_package = 'language-pack-%s' % langInfo.languageCode
- if self._cache.has_key(trans_package) and self._cache[trans_package].isInstalled:
- for pkg, translation in self._cache.pkg_translations:
- missing += self.missingTranslationPkgs(pkg, translation + langInfo.languageCode)
-
-
- default_lang = self.getSystemDefaultLanguage()
- if default_lang is None:
- return missing
- if '_' in default_lang:
- default_lang = default_lang.split('_')[0]
-
- trans_package = 'language-pack-%s' % default_lang
- if self._cache.has_key(trans_package) and not (self._cache[trans_package].isInstalled):
- missing += [
- trans_package]
- for pkg, translation in self._cache.pkg_translations:
- missing += self.missingTranslationPkgs(pkg, translation + default_lang)
-
-
- support_packages = LanguageSelectorPkgCache._getPkgList(self._cache, default_lang)
- for support_package in support_packages:
- if self._cache.has_key(support_package) and not (self._cache[support_package].isInstalled):
- missing.append(support_package)
- continue
-
- return missing
-
-
- def getUserDefaultLanguage(self):
- fname = os.path.expanduser('~/.dmrc')
- if os.path.exists(fname):
- for line in open(fname):
- match = re.match('Language=(.*)$', line)
- if match:
- if '.' in match.group(1):
- return match.group(1).split('.')[0]
- return match.group(1)
- match
-
-
-
-
- def getSystemDefaultLanguage(self):
- conffiles = [
- '/etc/default/locale',
- '/etc/environment']
- for fname in conffiles:
- if os.path.exists(fname):
- for line in open(fname):
- match = re.match('LANG="(.*)"$', line)
- if match:
- if '.' in match.group(1):
- return match.group(1).split('.')[0]
- return match.group(1)
- match
-
-
-
-
- def runAsRoot(self, cmd):
- ''' abstract interface for the frontends to run specific commands as root'''
- if os.getuid() == 0:
- return subprocess.call(cmd)
- raise AttributeError, 'this method needs to be overwriten by the subclass'
-
-
- def setSystemDefaultLanguage(self, defaultLanguageCode):
- ''' this updates the system default language '''
- conffiles = [
- '/etc/default/locale',
- '/etc/environment']
- for fname in conffiles:
- out = tempfile.NamedTemporaryFile()
- foundLanguage = False
- foundLang = False
- if os.path.exists(fname):
- for line in open(fname):
- tmp = string.strip(line)
- if tmp.startswith('LANGUAGE='):
- foundLanguage = True
- line = 'LANGUAGE="%s"\n' % self._localeinfo.makeEnvString(defaultLanguageCode)
-
- if tmp.startswith('LANG='):
- foundLang = True
- line = 'LANG="%s.UTF-8"\n' % defaultLanguageCode
-
- out.write(line)
-
-
- if foundLanguage == False:
- line = 'LANGUAGE="%s"\n' % self._localeinfo.makeEnvString(defaultLanguageCode)
- out.write(line)
-
- if foundLang == False:
- line = 'LANG="%s.UTF-8"\n' % defaultLanguageCode
- out.write(line)
-
- out.flush()
- self.runAsRoot([
- '/bin/cp',
- out.name,
- fname])
-
- fc = FontConfig.FontConfigHack()
- if defaultLanguageCode in fc.getAvailableConfigs():
- self.runAsRoot([
- 'fontconfig-voodoo',
- '-f',
- '--set=%s' % defaultLanguageCode])
- else:
- self.runAsRoot([
- 'fontconfig-voodoo',
- '-f',
- '--remove-current'])
-
-
- def setUserDefaultLanguage(self, defaultLanguageCode):
- ''' this updates the user default language '''
- fname = os.path.expanduser('~/.dmrc')
- out = tempfile.NamedTemporaryFile()
- foundLang = False
- if os.path.exists(fname):
- for line in open(fname):
- tmp = string.strip(line)
- if tmp.startswith('Language='):
- foundLang = True
- line = 'Language=%s.UTF-8\n' % defaultLanguageCode
-
- out.write(line)
-
-
- if foundLang == False:
- line = 'Language=%s.UTF-8\n' % defaultLanguageCode
- out.write(line)
-
- out.flush()
- shutil.copy(out.name, fname)
- os.chmod(fname, 420)
-
-
- def missingTranslationPkgs(self, pkg, translation_pkg):
- ''' this will check if the given pkg is installed and if
- the needed translation package is installed as well
-
- It returns a list of packages that need to be
- installed
- '''
- missing = []
- if not self._cache.has_key(pkg):
- return missing
- if not self._cache[pkg].isInstalled:
- return missing
- for pkg in self._cache:
- if pkg.name == translation_pkg or pkg.name.startswith(translation_pkg + '-'):
- if not (pkg.isInstalled) and pkg.candidateVersion != None:
- missing.append(pkg.name)
-
- pkg.candidateVersion != None
-
- return missing
-
-
- if __name__ == '__main__':
- lsb = LanguageSelectorBase(datadir = '..')
- lsb.openCache(apt.progress.OpProgress())
- print lsb.verifyPackageLists()
-
-